package top.qiuzhe.entity;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.TrueFileFilter;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import top.qiuzhe.utils.LogUtils;

import java.io.File;
import java.util.*;

/**
 * @program: DocsifyManage
 * @description: 本地文件目录对象
 * @author: qiuzhe
 * @create: 2022-03-23 10:44
 **/

@Component
public class FilePage {

    public String dirPath;

    String dirName;

    Map<String,List<String>> map;

    public  FilePage(){
        map = new HashMap<>();
    }

    /**
     *
     * @param in 加载哪些后缀的文件
     * @param out 过滤哪些文件夹
     */
    public void loadFiles(String in,String out) {
        // TODO
        // 目前的获取方法只支持获取一级目录
        File file = new File(this.dirPath);
        dirName = file.getName();
        File[] list = file.listFiles();
        // 遍历的文件对象
        File f;
        // 遍历的文件名称
        String fileName;

        for (int i = 0; i < list.length; i++) {
            f = list[i];
            fileName = f.getName();
            if (f.isDirectory() && !fileName.startsWith(out)) {
                LogUtils.logInfo("FilePage. loadFiles：文件夹==>" + fileName);
                File[] childFiles = f.listFiles();
                map.put(fileName, new ArrayList<String>());
                for (int j = 0; j < childFiles.length; j++) {
                    if (!childFiles[j].isDirectory()) {
                        map.get(fileName).add(childFiles[j].getName());
                    }
                }
                continue;
            }

            if (!fileName.endsWith(in)) {
                continue;
            }
            map.put(fileName, null);
            LogUtils.logInfo("FilePage. loadFiles：文件==>" + fileName);
        }

            LogUtils.logInfo(map.toString());
    }
    public String getDirPath() {
        return dirPath;
    }

    public void setDirPath(String dirPath) {
        this.dirPath = dirPath;
    }
}